﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using BisinessLogic;
using Domain.Entities;
using Web.ViewModels;

namespace Web.Controllers
{
    [Authorize(Roles = "Moder,Administrator")]
    public class ManagerOrderController : Controller
    {
        private DataManager dataManager;

        public ManagerOrderController(DataManager dataManager)
        {
            this.dataManager = dataManager;
        }
       
        [HttpGet]
        public ActionResult BrowseNewOrders()
        {
            var list = dataManager.Orders.GetOrders().Where(i => i.OrderStatusId == StatusOrder.New);
            return View(list);
        }

        [HttpGet]
        public ActionResult BrowseOrdersInProcessing()
        {
            var list = dataManager.Orders.GetOrders().Where(i => i.OrderStatusId == StatusOrder.InProcessing);
            return View( list);
        }

        [HttpGet]
        public ActionResult BrowseExecutedOrders()
        {
            var list = dataManager.Orders.GetOrders().Where(i => i.OrderStatusId == StatusOrder.Executed);
            return View(list);
        }

        [HttpPost]
        public ActionResult DeleteOrder(int id)
        {
            try
            {
                var listDetails = dataManager.OrderDetails.GetOrderDetails().Where(i => i.OrderId == id);
                if (listDetails.Any())
                {
                    foreach (OrderDetail orderDetail in listDetails)
                    {
                        dataManager.OrderDetails.DeleteOrderDetailById(orderDetail.Id);
                    }
                }
                dataManager.Orders.DeleteOrderById(id);
            }
            catch (Exception)
            {
                return Json(new
                {
                    success = false,
                    errorMessage = "Произошла ошибка при удалении заказа"
                });
            }
            return Json(new { success = true });
        }



        [HttpGet]
        public ActionResult EditOrder(int id)
        {
            var or = dataManager.Orders.GetOrderById(id);
            var orDetails = dataManager.OrderDetails.GetOrderDetails().Where(i => i.OrderId == or.Id).ToList();

            List<ProduceModel> list = orDetails.Select(d => new ProduceModel
            {
                PrepackageId = d.ProduceInPackageId,
                Name = d.ProduceInPackage.Produce.Name,
                Package = d.ProduceInPackage.Prepackage.Weight.ToString(CultureInfo.InvariantCulture),
                Category = d.ProduceInPackage.Produce.Category.Name,
                Price = d.UnitPrice,
                Count = d.Quantity
            }).ToList();

            var model = new OrderViewModel
            {
                order = or,
                orderDetails = list
            };

            ViewBag.OrderStatusId = new SelectList(dataManager.OrderStatuses.GetOrderStatuses(), "Id", "Name", model.order.OrderStatusId);
            return View(model);
        }

        [HttpPost]
        public ActionResult EditOrder(OrderViewModel model)
        {
            
            if (ModelState.IsValid)
            {
                dataManager.Orders.SaveOrder(model.order);
            }
            var orDetails = dataManager.OrderDetails.GetOrderDetails().Where(i => i.OrderId == model.order.Id).ToList();

            List<ProduceModel> list = orDetails.Select(d => new ProduceModel
            {
                PrepackageId = d.ProduceInPackageId,
                Name = d.ProduceInPackage.Produce.Name,
                Package = d.ProduceInPackage.Prepackage.Weight.ToString(CultureInfo.InvariantCulture),
                Category = d.ProduceInPackage.Produce.Category.Name,
                Price = d.UnitPrice,
                Count = d.Quantity
            }).ToList();
            model.orderDetails = list;
            ViewBag.OrderStatusId = new SelectList(dataManager.OrderStatuses.GetOrderStatuses(), "Id", "Name", model.order.OrderStatusId);
            return View(model);
        }

        public ActionResult EditListOrder(int id)
        {
            var or = dataManager.Orders.GetOrderById(id);
            var orDetails = dataManager.OrderDetails.GetOrderDetails().Where(i => i.OrderId == or.Id).ToList();

            List<OrderDetailViewModel> list = orDetails.Select(d => new OrderDetailViewModel
            {
                Id = d.Id,
                Category = d.ProduceInPackage.Produce.Category.Name,
                Package = d.ProduceInPackage.Prepackage.Weight.ToString(CultureInfo.InvariantCulture),
                Producer = d.ProduceInPackage.Produce.Name,
                Quantity = d.Quantity,
                UnitPrice = d.UnitPrice
            }).ToList();

            var model = new EditOrderDetailViewModel
            {
                OrderId = or.Id,
                list = list
            };

            return View(model);
        }

        public ActionResult DeleteFromListOrder(int id)
        {
            try
            {
                var o = dataManager.OrderDetails.GetOrderDetailById(id);
                dataManager.Orders.GetOrders().Single(i => i.Id == o.OrderId).Total -= o.UnitPrice * o.Quantity;
                dataManager.OrderDetails.DeleteOrderDetailById(id);
            }
            catch (Exception)
            {
                return Json(new
                {
                    success = false
                });

            }
            return Json(new { success = true });
        }
        
        [HttpGet]
        public ActionResult AddToListOrder(int id)
        {
            var productCatalog = new ProduceCatalog
            {
                OrderId = id
            };
            return View(productCatalog);
        }
        
        [HttpPost]
        public ActionResult AddToListOrder(ProduceCatalog model)
        {
            if (ModelState.IsValid)
            {
                int pId = (model.SelectedPrepackageId.HasValue) ? (int)model.SelectedPrepackageId : 0;
                int prod = (model.SelectedProduceId.HasValue) ? (int)(model.SelectedProduceId) : 0;
                int storeId = GetStoreIdByProduceAndPackage(pId, prod);

                OrderDetail or = new OrderDetail
                {
                    OrderId = model.OrderId,
                    ProduceInPackageId = storeId,
                    Quantity = model.Count,
                    UnitPrice = dataManager.ProduceInPackages.GetProduceInPackageById(storeId).Price
                };

               dataManager.OrderDetails.CreateOrderDetail(or);



                var o = dataManager.Orders.GetOrderById(model.OrderId);
                o.Total =
                    dataManager.OrderDetails.GetOrderDetails()
                        .Where(i => i.OrderId == o.Id)
                        .Select(s => s.UnitPrice * s.Quantity)
                        .Sum();
                dataManager.Orders.SaveOrder(o);

            }

            return View(model);
        }


        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetJsonProduceByCategoryId(int id = 0)
        {

            var list = from p in dataManager.Produces.GetProduces()
                       where p.CategoryId == id
                       select new { id = p.Id, Text = p.Name };

            return Json(list, JsonRequestBehavior.AllowGet);

        }
        
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetJsonPackagesbyProduce(int id)
        {
            var list = (from p in dataManager.ProduceInPackages.GetProduceInPackages()
                        where p.ProduceId == id
                        orderby p.Prepackage.Weight
                        select new { id = p.Prepackage.Id, Text = p.Prepackage.Weight + " г" }).Distinct();
            return Json(list, JsonRequestBehavior.AllowGet);

        }

        int GetStoreIdByProduceAndPackage(int packId, int produceId)
        {

            return dataManager.ProduceInPackages.GetProduceInPackages()
                .FirstOrDefault(i => i.PrepackageId == packId && i.ProduceId == produceId).Id;
        }

        [HttpGet]
        public ActionResult EditItemFromOrderDetails(int id)
        {
            var or = dataManager.OrderDetails.GetOrderDetailById(id);
            var productCatalog = new ProduceCatalog
            {
                OrderId = or.OrderId,
                SelectedProduceId = or.ProduceInPackage.ProduceId,
                SelectedCategoryId = or.ProduceInPackage.Produce.Category.Id,
                SelectedPrepackageId = or.ProduceInPackageId,
                Count = or.Quantity,
                OrderDetailId = or.Id
            };
            return View(productCatalog);

        }
    }
}
